// b2c/tc/tc.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    https: "https://csb2c.fenxiangqu.cn",
    cookie:null,
    indicatorDots: true,
    autoplay: true,
    interval: 2500,
    duration: 1000,
    tc:[],
    jan:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.setNavigationBarTitle({
      title: '组合套餐选购',
    })
    var a = wx.getStorageSync('cookie') || [];
    const self = this;
    this.setData({
      cookie: a
    })
    this.captc(res => {
      if (res.data[0].length > 0) {
        console.log(res)
        self.setData({
          tc: res.data[0],
          jan:false
        })
      }else{
        self.setData({
          tc: [],
          jan: true
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function (res) {
    
  },
  captc(callback) {
    const self = this;
    wx.request({
      url: "https://csb2c.fenxiangqu.cn/xcx_combo_silid",
      data: {
        id: ""
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'Cookie': "LID=" + self.data.cookie
      },
      method: "POST",
      success: (res) => {
        callback && callback(res)
      },
      fail: (res) => {
        wx.showToast({
          title: '网络请求失败',
          icon: 'success',
          duration: 1000
        })
      }
    })
  },
  gwc(e){
    const self = this;
    const id = e.currentTarget.dataset.id;
    console.log(id)
    wx.request({
      url: "https://csb2c.fenxiangqu.cn/xcx_combo_addGwc",
      data: {
        xl:1,
        id: id
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'Cookie': "LID=" + self.data.cookie
      },
      method: "POST",
      success: (res) => {
        if(res.data == "login"){
          wx.showModal({
            title: '提示',
            content: '您未登录，是否前往登录？',
            success: function (res) {
              if (res.confirm) {
                wx.redirectTo({
                  url: '../center/center',
                })
              } else if (res.cancel) {
                console.log('用户点击取消')
                wx.navigateBack()
              }
            }
          })
        }else{
          let obj = res.data;
          if (obj.error != undefined){
            wx.showToast({
              title: '网络请求失败',
              icon: 'success',
              duration: 1000
            })
          }else{
            wx.showToast({
              title: '添加成功',
              icon: 'success',
              duration: 1000
            })
            self.aaa()
          }
        }
      },
      fail: (res) => {
        wx.showToast({
          title: '网络请求失败',
          icon: 'success',
          duration: 1000
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },
  aaa(){
    const self = this;
    wx.request({
      url: "https://csb2c.fenxiangqu.cn/xcx_50010_wx",
      data: {
        id: ""
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'Cookie': "LID=" + self.data.cookie
      },
      method: "POST",
      success: (res) => {
        if(res.data == "login"){
          wx.redirectTo({
            url: '../home/home'
          })
        }
      },
      fail: (res) => {
        wx.showToast({
          title: '网络请求失败',
          icon: 'success',
          duration: 1000
        })
      }
    })
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})